<?php

namespace Common\Model;

/**
 * Description of RoomTypeModel
 * @author songanwei
 * @date 2015-12-31
 * @index roomtype(`isdelete`, `hotelentity_id`)
 */
class RoomTypeModel extends BaseModel {
 
    protected $trueTableName = 'tb_room_type';

    /**
     * 添加房型
     * @modify songanwei 2015-12-31
     * @modify wenhui 2017-09-14 新增义卖字段 $charityPrice
     * @param integer $hotelId
     * @param string $name
     * @param integer $price
     * @param integer $charityPrice
     * @return type
     */
    public function addInfo($hotelId, $name, $price, $charityPrice) {
        $options = array(
            'hotelentity_id' => $hotelId,
            'name' => $name,
            'price' => $price,
        );
        if (!is_null($charityPrice)) {
            $options['charityprice'] = $charityPrice;
        }

        return $this->add($options);
    }

    /**
     * 根据ID获取信息
     * @modify trainyao 2016-05-10  创建
     * @param  mixin   $roomTypeId 房型IDs
     * @return array               信息数组
     */
    public function getInfo($roomTypeId) {
        $option = array();
        if (is_array($roomTypeId)) {
            $roomTypeId = implode(',', $roomTypeId);
        }
        if (strpos($roomTypeId, ',') !== false) {
            $option['id'] = array('in', $roomTypeId);
        } else {
            $option['id'] = $roomTypeId;
        }
        $option['isdelete'] = NOT_DELETE;

        if (strpos($roomTypeId, ',') === false) {
            $data = $this->where($option)->find();
        } else {
            $data = $this->where($option)->select();
        }
        return empty($data) ? array() : $data;
    }

    /**
     * 获取客栈房型
     * @modify trainyao 2016-01-04 创建
     * @modify trainyao 2016-04-25 修改为可获取已删除的
     * @param  integer   $hotelId   客栈ID
     * @param  mixin   $getDeleted  是否获取已删除的
     * @return array 房型数组
     */
    public function getInfoByHotel($hotelId, $getDeleted = false) {
        if ($getDeleted !== false) {
            $option = array(
                'isdelete' => array('in', array(NOT_DELETE, IS_DELETE)),
                'hotelentity_id' => $hotelId,
            );
        } else {
            $option = array(
                'isdelete' => NOT_DELETE,
                'hotelentity_id' => $hotelId,
            );
        }

        if (is_array($hotelId)) {
            $option['hotelentity_id'] = array('in', $hotelId);
        }

        $field = 'id,isdelete,hotelentity_id,name,price,hourinit,charityprice,minusdiscount,minustype';
        $data = $this->where($option)->field($field)->select();

        return empty($data) ? array() : $data;
    }

    /**
     * 修改房型信息
     * @modify trainyao 2016-01-04     创建
     * @param  integer   $hotelId       客栈id
     * @param  integer   $roomTypeId    房型id
     * @param  string   $roomTypeName  房型名称
     * @param  string   $roomTypePrice 房型默认价格
     * @param  double   $minusDiscount 立减优惠或按比例优惠值
     * @param  double   $minusType 立减优惠方式 0=没优惠 1=立减优惠金额 2=按比例优惠
     * @return boolean 执行结果
     */
    public function editInfo($hotelId, $roomTypeId, $roomTypeName, $roomTypePrice, $discount, $charityPrice, $minusDiscount, $minusType) {
        $option = array(
            'isdelete' => NOT_DELETE,
            'hotelentity_id' => $hotelId,
            'id' => $roomTypeId,
        );

        $data = array();
        if(!empty($roomTypeName)) {
            $data['name'] = $roomTypeName;
        }
        if(!empty($roomTypePrice)) {
            $data['price'] = $roomTypePrice;
        }
        if(!is_null($charityPrice)) {
            $data['charityprice'] = $charityPrice;
        }
        if(!is_null($discount)) {
            $data['discount'] = $discount;
        }
        if(!is_null($minusDiscount)) {
            $data['minusdiscount'] = $minusDiscount;
        }
        if(!is_null($minusType)) {
            $data['minustype'] = $minusType;
        }

        return $this->where($option)->save($data) === false ? false : true;
    }

    /**
     * 修改钟点房价格
     * @modify zhangzhen 2017-07-27     创建
     * @param  integer  $roomTypeId    房型id
     * @param  string   $init  初始化
     * @return boolean 执行结果
     */
    public function saveHourRoomPriceId($roomTypeId, $init = 1) {
        $option = [
            'id' => $roomTypeId,
        ];
        $data = [
            'hourinit' => $init,
        ];
        return $this->where($option)->save($data) === false ? false : true;
    }

    /**
     * 添加长租房价格
     * @modify zhangzhen 2017-07-27     创建
     * @param int $roomTypeId 房型id
     * @param $price
     * @return bool 执行结果
     */
    public function saveLongRoomPrice($roomTypeId, $price) {
        $option = [
            'id' => $roomTypeId,
        ];
        $data = [
            'longprice' => $price,
        ];
        return $this->where($option)->save($data) === false ? false : true;
    }

    /**
     * 修改钟点房价格
     * @modify zhangzhen 2017-07-27     创建
     * @param  integer  $roomTypeId    房型id
     * @param  string   $init  初始化
     * @return boolean 执行结果
     */
    public function getLongPrice($roomTypeId) {
        $option = [
            'id' => $roomTypeId,
        ];
        return $this->field('longprice')->where($option)->find();
    }

    /**
     * 修改钟点房价格
     * @modify trainyao 2016-01-04     创建
     * @param  integer  $roomTypeId    房型id
     * @return boolean 执行结果
     */
    public function deleteInfo($roomTypeId) {
        $option = [
            'id' => $roomTypeId,
        ];
        $data = [
            'hourinit' => 0,
            'isdelete' => 1,
        ];
        return $this->where($option)->save($data) === false ? false : true;
    }
}
